package com.dm.hdfxxt.cylmanage.service.impl;

import com.dm.hdfxxt.cylmanage.mapper.CylMapper;
import com.dm.hdfxxt.cylmanage.service.CylService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
     * @Description 
     * @Author zhangxue
     * @Date 14:18 2018/12/11
     * @Param
     * @retrun
     **/
@Service
@Transactional(rollbackFor=Exception.class)
public class CylServiceImpl implements CylService {
    @Autowired
    private CylMapper cylMapper;

    /**
         * @Description 保存经销商信息
         * @Author zhangxue
         * @Date 11:19 2018/10/30
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> saveCyl(Map<String, Object> param) {
        Map<String,Object> StatusMap = new HashMap<String, Object>();
        List<Map<String,Object>> GiftList = cylMapper.queryCylByCondition(param);
        if(GiftList.size()>0){
            //参与率项目已存在
            StatusMap.put("Status",-1);
            return StatusMap;
        }
        int i = cylMapper.saveCyl(param);
        if(i==1){
            //保存成功
            StatusMap.put("Status",1);
        }else{
            //如果不是影响一条，直接回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            //保存失败
            StatusMap.put("Status",0);
        }
        return StatusMap;
    }

    /**
     * @Description 编辑参与率项目信息
     * @Author zhangxue
     * @Date 11:19 2018/10/30
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> editCyl(Map<String, Object> param) {
        Map<String,Object> StatusMap = new HashMap<String, Object>();
        int i = cylMapper.editCyl(param);
        if(i==1){
            StatusMap.put("Status",1);//编辑成功
        }else{
            //如果不是影响一条，直接回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            StatusMap.put("Status",0);//保存失败
        }
        List<Map<String,Object>> GiftList = cylMapper.queryCylByCondition(param);
        if(GiftList.size()>1){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            StatusMap.put("Status",-1);//经销商已存在
            return StatusMap;
        }
        return StatusMap;
    }

    /**
     * @Description 删除参与率项目信息
     * @Author zhangxue
     * @Date 11:19 2018/10/30
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> delCyl(Map<String, Object> param) {
        Map<String,Object> StatusMap = new HashMap<String, Object>();
        int i = cylMapper.delCyl(param);
        if(i==1){
            StatusMap.put("Status",1);//删除成功
        }else{
            //如果不是影响一条，直接回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            StatusMap.put("Status",0);//删除失败
        }
        return StatusMap;
    }

    /**
         * @Description 查询所有参与率项目
         * @Author zhangxue
         * @Date 11:36 2018/10/30
         * @Param
         * @retrun
         **/
    @Override
    public List<Map<String, Object>> queryAllCyl(Map<String, Object> param) {
        List<Map<String,Object>> ResultList = cylMapper.queryAllCyl(param);
        return ResultList;
    }

    /**
         * @Description 保存参与率项目业务数据
         * @Author zhangxue
         * @Date 11:30 2018/11/23
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> saveCylServiceInfo(Map<String, Object> param) {
        Map<String,Object> StatusMap = new HashMap<String, Object>();
        List<Map<String,Object>> queryList = cylMapper.queryExistCylServiceInfo(param);
        int i = 0;
        if(queryList.size()==0){
            i = cylMapper.saveCylServiceInfo(param);
        }else if(queryList.size()==1){
            i = cylMapper.updateCylServiceInfo(param);
        }
        if(i==1){
            StatusMap.put("Status",1);//保存成功
        }else{
            //如果不是影响一条，直接回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            StatusMap.put("Status",0);//保存失败
        }
        return StatusMap;
    }

    /**
         * @Description 添加参与订单总数
         * @Author zhangxue
         * @Date 14:55 2018/11/23
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> saveCylTotal(Map<String, Object> param) {
        Map<String,Object> StatusMap = new HashMap<String, Object>();
        List<Map<String,Object>> queryList = cylMapper.queryExistCylTotal(param);
        int i = 0;
        if(queryList.size()==0){
            i = cylMapper.saveCylTotal(param);
        }else if(queryList.size()==1){
            i = cylMapper.updateCylTotal(param);
        }
        if(i==1){
            StatusMap.put("Status",1);//保存成功
        }else{
            //如果不是影响一条，直接回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            StatusMap.put("Status",0);//保存失败
        }
        return StatusMap;
    }

    @Override
    public Map<String, Object> queryCylCaculate(Map<String, Object> param) {
        Map<String,Object> resultMap = new HashMap<String, Object>();
        List<Map<String,Object>> ServiceResultList = cylMapper.queryAllCylService(param);
        resultMap.put("ServiceResultList",ServiceResultList);
        //得到参与量CylServiceTotal
        int CylServiceTotal = 0;
        for(int i=0;i<ServiceResultList.size();i++){
            CylServiceTotal += Integer.valueOf(ServiceResultList.get(i).get("CyNum").toString());
        }
        //查询订单总量CylTotal
        List<Map<String,Object>> TotalResultList = cylMapper.queryAllCylTotal(param);
        int CylTotal = 0;
        for(int i=0;i<TotalResultList.size();i++){
            CylTotal += Integer.valueOf(TotalResultList.get(i).get("CyNum").toString());
        }
        //计算参与率
        Double CylRate = Double.valueOf(CylServiceTotal)/Double.valueOf(CylTotal);
        CylRate = CylRate*100;
        String CylRateStr = String.format("%.2f", CylRate);
        resultMap.put("CylServiceTotal",CylServiceTotal);
        resultMap.put("CylTotal",CylTotal);
        resultMap.put("CylRate",CylRateStr);
        return resultMap;
    }
}
